const wxToken = wx.getStorageSync("wxToken");
const ajax = (url, data, method, message) => {
    return new Promise((resolve, reject) => {
        wx.request({
            url,
            data,
            header: {wxToken},
            method,
            success({data}) {
                if (data.code === 200) {
                    resolve(data.data)
                } else {
                    if (message) {
                        wx.showModal({
                            title: "错误提示",
                            content: data.msg,
                            showCancel: false,
                            success(res) {
                                if (res.confirm) {
                                    reject(data)
                                }
                            }
                        })
                    } else {
                        reject(data)
                    }
                }
            },
            error(e) {
                wx.showModal({
                    title: "错误提示",
                    content: e.msg,
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            reject(e)
                        }
                    }
                })
            }
        })
    })
}
const http = {
    baseUrl: "http://localhost:9898/book/",
    get(url, data = {}, message = true) {
        return ajax(this.baseUrl + url, data, "GET", message);
    },
    post(url, data = {}, message = true) {
        return ajax(this.baseUrl + url, data, "POST", message);
    },
    delete(url, data = {}, message = true) {
        return ajax(this.baseUrl + url, data, "DELETE", message);
    }
}
module.exports = http
